package com.edu.web.shop;

import com.edu.entity.shop.Comments;
import com.edu.entity.shop.Video;
import com.edu.service.shop.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/video")
public class VideoController {
//    private final static int SIZE =4;
    @Autowired
    private VideoService videoService;
//    分页
    @RequestMapping("/findListByName")
    public String findListByName(Model model, String brief,Integer page,Integer size){
//        page =1;
        Integer count=videoService.findCount(brief);
        List<Video> videos=videoService.findListByName(brief,page,size);
        System.out.println(videos);//

        model.addAttribute("videos",videos);
        model.addAttribute("count",count);
        model.addAttribute("curr",page);

        return "shop/list";

    }
//    收藏
    @ResponseBody
    @RequestMapping("/collect")
    public String select(Principal principal,Integer id){
        Integer prinId=videoService.findUserByPrin(principal.getName()).getId();
        videoService.insertCollect(id,prinId,new Date());
        return "1";

    }


    @RequestMapping("/shophome")
    public String indexRecent(Model model){
//        加载即查询新品推荐
        List<Video> videos1=videoService.findVideoRecent();
        List<Video> videos2=videoService.findVideoMost();
//        加载即查询精品推荐
        model.addAttribute("videosRecent",videos1);
        model.addAttribute("videosMost",videos2);
        return "shop/index";
    }
//      详情
    @RequestMapping("/preview")
    public String priview(Model model, Integer id){
        Video video=videoService.findVideoById(id);
        model.addAttribute("video",video);
        List<Video> videos=videoService.findListByTag2(video.getTag());
        model.addAttribute("videos",videos);
        return "shop/preview";
    }
//      播放
    @RequestMapping("/playback_page")
    public String playback_page(Model model, Integer id) throws ParseException {
        //第一步：查询当前点击的视频源
        //第二步：根据系列id查询相关的所有视频
        Video videoSingle=videoService.findVideoById(id);
        model.addAttribute("videoSingle",videoSingle);
//        找到pid
        Integer pid=videoService.findPid(id);
        System.out.println(pid);
        List<Video> videos1=videoService.findVideoPid(pid);
        if(videos1!=null){
            if(videos1.size()==1){
                videos1.get(0).setVideoname(videos1.get(0).getTag());
            }
            List<Video> videos2=videoService.findListByTag(videos1.get(id).getTag());

            model.addAttribute("videos2",videos2);
        }
        model.addAttribute("videos1",videos1);

        List<Comments> comments=videoService.findCommentsById(id);
        model.addAttribute("comments",comments);
        return "shop/playback_page";
    }

    @RequestMapping("/findGraAndSub")
    public String findGraAndSub(Model model, String grade,String subject,Integer page,Integer size){
        Integer count=videoService.findGraAndSubCount(grade,subject);
        List<Video> videos=videoService.findGraAndSubListByName(grade,subject,page,size);
        System.out.println(videos);//

        model.addAttribute("videos",videos);
        model.addAttribute("count",count);
        model.addAttribute("curr",page);

        return "shop/list";
    }
//      支付
    @RequestMapping("/pay")
    public String pay(Model model){
        return "shop/pay";
    }
//      完成
    @RequestMapping("/ok")
    public String ok(Model model){
        return "shop/ok";
    }
}

